﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;
using System.Data;
using System.Configuration;

public partial class admin_addNews : System.Web.UI.Page
{
    db_news news = new db_news();
    db_slide slide = new db_slide();
    protected void Page_Load(object sender, EventArgs e)
    {
        Server.Execute("inc/checkPurview.aspx");  // 此处用户权限验证

        if (!IsPostBack)
        {
            //ddl_newskind.DataSource = ds.Tables[0].DefaultView;
            //ddl_newskind.DataTextField = "kindName";
            //ddl_newskind.DataValueField = "ID";
            //ddl_newskind.DataBind();
            //ddl_newskind.Items.Insert(0, new ListItem("请选择栏目", ""));
            //ddl_newskind.Items[0].Selected = true;
        }
    }
    protected void btn_ok_Click(object sender, EventArgs e)
    {
        
            string title = tb_title.Text.Trim();
            //string body = tb_body.Text.Replace("\r\n", "").Trim();
            string body = tb_body.Text.Replace("\'", "''");
            string[] picUrls = Tools.GetHtmlImageUrlList(body);
            string isPic;
            string picUrl;
            bool isSlide;
            string slidetext = "";
            if (picUrls.Length > 0)
            {
                isPic = "0";
                picUrl = picUrls[0];
                string nohtml = Tools.NoHTML(body);
                if (nohtml.Length > 20)
                {
                    slidetext = nohtml.Substring(0, 20) + "……";
                }
                else
                {
                    slidetext = nohtml + "……";
                }
                isSlide = true;
                //Response.Write("<script language=javascript>alert('yes！');</script>");
            }
            else
            {
                isPic = "1";
                picUrl = "";
                isSlide = false;
                //Response.Write("<script language=javascript>alert('no！');</script>");
            }


            if (news.addNews(title, body, isPic, picUrl))
            {
                if (isSlide)
                {
                    string title_id = news.selectNewsTop().Tables[0].Rows[0][0].ToString();
                    if (slide.addSlide(picUrl, title, slidetext, "/news.aspx?id=" + title_id))
                    {
                        Response.Write("<script language=javascript>alert('新闻发布成功！');location.href='manageNews.aspx';</script>");
                    }
                    else
                        Response.Write("<script language=javascript>alert('失败！');</script>");
                }
                else
                {

                    Response.Write("<script language=javascript>alert('新闻发布成功！');location.href='manageNews.aspx';</script>");
                }
            }
            else
            {
                Response.Write("<script language=javascript>alert('失败！');</script>");
            }
            
        
    }
    protected void btn_res_Click(object sender, EventArgs e)
    {
        tb_body.Text = "";
        tb_title.Text = "";
    }
}